﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Xml.Linq;
using System.IO;
using System.Threading;

namespace GSync
{

    public interface ISettingsManager
    {
        string AppVersion
        { get; }
        string ServerAccountName
        {
            get; 
            set;
        }
        string ServerPassword
        {
            get;
            set;
        }

        string LicenseKey
        {
            get;
            set;
        }
        string DeviceStamp
        {
            get; 
        }

        TimeSpan SyncPeriod
        { 
            get; 
            set; 
        }

        string TraceFile
        {
            get;
        }

        bool SyncContacts
        { get; set; }
        bool SyncCalendar
        { get; set; }

        void Load();
        void Save();
    }


    public class SettingsManagerStub : ISettingsManager
    {
        public string AppVersion
        {
            get
            { 
                AssemblyName  name = Assembly.GetExecutingAssembly().GetName();
                string result = string.Format(string.Format("{0} {1}", name.Name, name.Version));
                return result;
            }
        }



        #region ISettingsManager Members


        public string ServerAccountName
        {
            get
            {
                return "gsynctester@gmail.com";
            }
            set
            {
                
            }
        }

        public string ServerPassword
        {
            get
            {
                return "1qazxsw2";
            }
            set
            {
               
            }
        }


        public string LicenseKey
        {
            get
            {
                return Guid.Empty.ToString("N");
            }
            set
            {
                
            }
        }

        public string DeviceStamp
        {
            get 
            {
                string result =
                    Environment.OSVersion.ToString().GetHashCode().ToString() +
                    Environment.Version.ToString().GetHashCode().ToString() +
                    AppVersion.GetHashCode().ToString();

                return result;
            
            }
        }

        public TimeSpan SyncPeriod
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public void Load()
        {
            throw new NotImplementedException();
        }

        public void Save()
        {
            throw new NotImplementedException();
        }

        #endregion

        #region ISettingsManager Members


        public bool SyncContacts
        {
            get
            {
                return true;
            }
            set
            {
               
            }
        }

        public bool SyncCalendar
        {
            get
            {
                return true;
            }
            set
            {

            }
        }

        public string TraceFile
        {
            get
            {
                string result = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                result = System.IO.Path.Combine(result, "gsync.trace");
                return result;
            }
        }

        #endregion
    }

    public class PocketPCSettingsManager : ISettingsManager
    {
       
        object syncObject = new object();
        protected string ConfigFilePath
        {
            get
            {
                string result = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                result = System.IO.Path.Combine(result, "gsync.config");
                return result;
            }
        }

        XDocument doc;
        #region ISettingsManager Members

        public string AppVersion
        {
            get
            {
                AssemblyName name = Assembly.GetExecutingAssembly().GetName();
                string result = string.Format(string.Format("{0} {1}", name.Name, name.Version));
                return result;
            }
        }

        public string ServerAccountName
        {
            get
            {
                return GetElementValue("ServerAccountName");
            }
            set
            {

                SetElementValue("ServerAccountName", value);
            }
        }

        protected string GetElementValue(string name)
        {
            lock (syncObject)
            {
                if (doc.Root.Element(name) != null)
                    return doc.Root.Element(name).Value;

                return string.Empty;                
            }

        }

        protected void SetElementValue(string name, string value)
        {
            lock (syncObject)
            {
                if (doc.Root.Element(name) == null)
                {
                    doc.Root.Add(new XElement(name));
                }

                doc.Root.Element(name).Value = value;
            }
        }

        public string ServerPassword
        {
            get
            {
                return GetElementValue("ServerPassword");
            }
            set
            {
                SetElementValue("ServerPassword", value);
            }
        }

        public string LicenseKey
        {
            get
            {
                return GetElementValue("LicenseKey");
            }
            set
            {
                SetElementValue("LicenseKey", value);
            }
        }

        public string DeviceStamp
        {
            get {
                string result =
                        Environment.OSVersion.ToString().GetHashCode().ToString() +
                        Environment.Version.ToString().GetHashCode().ToString() +
                        AppVersion.GetHashCode().ToString();

                return result;
            
            }
        }

        public TimeSpan SyncPeriod
        {
            get
            {
                string tmp = GetElementValue("SyncPeriod");
                if( !string.IsNullOrEmpty(tmp) )
                {
                    return TimeSpan.Parse(tmp);
                }
                else 
                    return new TimeSpan(3,0,0);
            }
            set
            {
                SetElementValue("SyncPeriod",  value.ToString() );   
            }
        }

        public void Load()
        {
            lock (syncObject)
            {
                if (File.Exists(ConfigFilePath))
                    doc = XDocument.Load(ConfigFilePath);
                else
                {
                    doc = new XDocument();
                    doc.Add(new XElement("GSync"));
                }
            }

        }

        public void Save()
        {
            lock (syncObject)
            {
                doc.Save(ConfigFilePath);
            }
        }

        #endregion

        #region ISettingsManager Members


        public bool SyncContacts
        {
            get
            {
                string val = GetElementValue("SyncContacts");
                if( string.IsNullOrEmpty(val ) )
                {
                    return true;
                }
                return bool.Parse(val);
            }
            set
            {
                SetElementValue("SyncContacts", value.ToString());
            }
        }

        public bool SyncCalendar
        {
            get
            {
                string val = GetElementValue("SyncCalendar");
                if (string.IsNullOrEmpty(val))
                {
                    return true;
                }
                return bool.Parse(val);
            }
            set
            {
                SetElementValue("SyncCalendar", value.ToString());
            }
        }

        #endregion

        #region ISettingsManager Members


        public string TraceFile
        {
            get
            {
                string result = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                result = System.IO.Path.Combine(result, "gsync.trace");
                return result;
            }
        }

        #endregion
    }

}
